<template>
	<view>
		<view class="blue-bg">
			<view class="keywords-container">
				<view class="keywords-top">
					<view class="section-head-line"></view>请填写产品或行业 （<text>{{tagList.length}}</text>/<text>10</text>）
				</view>
				<view class="keywords-tips">填写更多主营产品，商机更全哦！</view>
				<robby-tags v-model:tagList="tagList" @add="addTag" @delete="delTag" @click="clickTag" :enable-del="enableDel"
					:enable-add="enableAdd" ref="childNav" @loadTuijian="loadTuijian"></robby-tags>
				<view class="recomm-box">
					<view class="recomm-box-title">推荐关键词：</view>
					<view class="recomm-keywords">
						<view class="keyword" v-for="(word,index) in keywords" :key="index">
							<text class="recomm-key-item" @click="addTuijian(word)">{{word}}</text>
						</view>
					</view>
				</view>
				<view class="keywords-save">
					<button type="primary" @click="save">保存</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import robbyTags from '@/components/robby-tags/robby-tags.vue'
	export default {
		components: {
			robbyTags
		},
		data() {
			return {
				enableDel: true,
				enableAdd: true,
				colorType: 'primary',
				tagList: [],
				keywords: [],
			};
		},
		onLoad: function(options) {
			var keyword = options.keyword;
			if (keyword != '' && keyword != undefined) {
				var keyarr = keyword.split(',');
				this.tagList = keyarr;
				console.log(this.tagList);
			}
			this.loadkeyword("");
		},
		methods: {
			clickTag: function(e) {
				//console.log(e)
			},
			delTag: function(e) {
				//console.log(e)
			},
			addTag: function(e) {
				//console.log(e)
				this.loadTuijian(e.currentTag.toString())
			},
			save: function(e) {
				console.log(e);
				var pages = getCurrentPages();
				var prevPage = pages[pages.length - 2]; //上一个页面
				prevPage.keywordedit = this.tagList.toString();
				if (prevPage.keywordedit.length > 80) {
					uni.showToast({
						title: "所有的关键词总长度不能超过" + prevPage.keywordedit.length + "/80个字",
						icon: "none"
					});
					return false;
				}
				uni.navigateBack();
			},
			addTuijian: function(word) {
				this.$refs.childNav.tuijianCreateTag(word);
			},
			loadTuijian(keyword) {
				if (keyword != '' && keyword != undefined)
					this.loadkeyword(keyword)
			},
			loadkeyword(keyword) {
				//异步请求数据
				let data = this.initNewData();
				data.location = 8086;
				data.keyword = keyword;
				uni.request({
					url: this.baseConfig.serverURL + 'user/custom/searchRelatedHandler.ashx', //检查更新的服务器地址
					data: data,
					method: "POST",
					header: {
						"content-type": "application/x-www-form-urlencoded" //"application/json"
					},
					success: (res) => {
						if (res.statusCode == 200) {
							if (res.data.ret) {
								//获取成功，绑定
								let tuijian = res.data.other2;
								if (tuijian != undefined && tuijian.length > 0) {
									this.keywords = tuijian;
								}
								console.log(tuijian);
							}
						}
					},
					fail: (res) => {
						uni.showToast({
							title: '获取失败，可能已超时！',
							icon: "none"
						});
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ebebeb;
		height: 100%;
	}

	.blue-bg {
		background-color: #3b8bfe;
		height: 100rpx;
	}

	.keywords-container {
		background-color: #fff;
		border-radius: 20rpx;
		margin: 0 30rpx;
		padding-top: 20rpx;
		padding-bottom: 40rpx;
	}

	.keywords-top {
		font-size: 30rpx;
		//margin-left: 20rpx;
		padding-left: 20rpx;
		display: flex;
		flex-direction: row;
		align-items: center;

		.section-head-line {
			width: 8rpx;
			height: 34rpx;
			background-color: #2d67e0;
			border-radius: 5rpx;
			margin-right: 10rpx;
		}
	}


	.keywords-tips {
		font-size: 28rpx;
		color: #fb1a1a;
		margin: 15rpx 20rpx;
	}

	.keywords-save {
		display: flex;
		flex-direction: column;
		justify-content: center;
		margin-top: 40rpx;

		button {
			width: 90%;
			border-radius: 50rpx;
			background-color: #3b8bfe;
			font-size: 30rpx;
		}
	}

	.recomm-box {
		margin-top: 20rpx;

		.recomm-box-title {
			font-size: 30rpx;
			margin: 0 20rpx;
		}
	}

	.recomm-keywords {
		display: flex;
		flex-direction: row;
		align-items: center;
		flex-wrap: wrap;
		margin: 0 20rpx;

		.keyword {
			display: flex;
			margin: 15rpx 10rpx 0 0;

			.recomm-key-item {
				padding: 0 30rpx;
				background-color: #f1f1f1;
				height: 58rpx;
				line-height: 58rpx;
				text-align: center;
				color: #555555;
				font-size: 28rpx;
				border-radius: 10rpx;
			}
		}
	}

	.product-list {
		display: flex;
		flex-direction: row;
		align-items: center;
		flex-wrap: wrap;
		margin: 0 20rpx 20rpx;

		.product-list-item {
			margin: 15rpx 10rpx 0 0;
			padding: 0 20rpx;
			background-color: #3b8bfe;
			height: 70rpx;
			line-height: 70rpx;
			text-align: center;
			color: #fff;
			font-size: 24rpx;
			border-radius: 10rpx;

			.product-delete {
				width: 24rpx;
				height: 24rpx;
				vertical-align: middle;
				margin-left: 20rpx;
			}
		}
	}
</style>
